package com.test.controllers;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.test.services.LoginUserService;

/**
 * @author Blackvault
 * 
 */
public class LoginActivity extends Activity {

	private static final String TAG = "LoginActivity";

	private Button btnLogin;
	private Button btnRegister;
	private LoginUserService loginUserServiceObj = new LoginUserService();

	private String password = "";
	private String username = "";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);

		final EditText txtUserName = (EditText) findViewById(R.id.txtUname);
		final EditText txtPassword = (EditText) findViewById(R.id.txtPassword);

		txtUserName.setFocusable(true);

		btnLogin = (Button) findViewById(R.id.btnLogin);
		btnLogin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				username = txtUserName.getText().toString();
				Log.d(TAG, "Username: " + username);

				password = txtPassword.getText().toString();
				Log.d(TAG, "Password: " + password);

				if (username.matches("")) {
					dialogBox("Please fill in the username field.",
							"No Username Entered.");
					txtUserName.setFocusable(true);
					Log.d(TAG, "No Username entered ");
				} else if (password.matches("")) {
					dialogBox("Please fill in the password field.",
							"No Password Entered.");
					txtPassword.setFocusable(true);
					Log.d(TAG, "No password entered ");

				} else if (username.matches("") && password.matches("")) {
					dialogBox(
							"Please fill in both username and password fields.",
							"No Username or Password entered.");
					Log.d(TAG, "No Username or Password entered ");

				} else {

					String loginResult = "";

				
					loginResult = loginUserServiceObj
							.ValidateUser(username, password);

					if (loginResult.matches("1")) {

						
						
						
						Log.d(TAG, "Preparing intent to Main Menu Activity");

						Intent intent = new Intent(LoginActivity.this,
								MainMenuActivity.class);

					
						
						intent.putExtra("username", username);
						Log.d(TAG, "Intent Username: " + username);
						intent.putExtra("password", password);
						Log.d(TAG, "Intent password: " + password);

						Log.d(TAG, "Starting intent to Main Menu Activity");
						startActivity(intent);

					} else {
						dialogBox(
								"Please check your credentials are correct or register.",
								"No User Found.");
						Log.d(TAG, "No user found in the database.");

					}
				}

			}

		});

		btnRegister = (Button) findViewById(R.id.btnRegister);
		btnRegister.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Log.d(TAG, "Preparing intent to Registration1Activity");
				Intent intent = new Intent(LoginActivity.this,
						Registration1Activity.class);

				Log.d(TAG, "Starting intent to Registration1Activity");
				startActivity(intent);

			}
		});

	}

	private void dialogBox(String messageDisplayText, String Title) {

		String msg = messageDisplayText;
		Log.d(TAG, "Message to be displayed" + msg);

		String title = Title;
		Log.d(TAG, "Title to be displayed" + title);

		AlertDialog.Builder builder = new AlertDialog.Builder(
				LoginActivity.this);

		builder.setMessage(msg).setCancelable(false)
				.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int id) {

					}
				});
		AlertDialog alert = builder.create();
		alert.setTitle(title);
		alert.show();
	}

}
